package com.numberone.master.modules.agent.web;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.numberone.master.common.config.Global;
import com.numberone.master.common.web.BaseController;
import com.numberone.master.common.utils.StringUtils;
import com.numberone.master.modules.agent.entity.Agent;
import com.numberone.master.modules.agent.service.AgentService;

/**
 * 代理商Controller
 * @author chengkang
 * @version 2018-01-23
 */
@Controller
@RequestMapping(value = "${adminPath}/agent/agent")
public class AgentController extends BaseController {

	@Autowired
	private AgentService agentService;
	
	@ModelAttribute
	public Agent get(@RequestParam(required=false) String id) {
		Agent entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = agentService.get(id);
		}
		if (entity == null){
			entity = new Agent();
		}
		return entity;
	}
	
	@RequiresPermissions("agent:agent:view")
	@RequestMapping(value = {"list", ""})
	public String list(Agent agent, HttpServletRequest request, HttpServletResponse response, Model model) {
		List<Agent> list = agentService.findList(agent); 
		model.addAttribute("list", list);
		return "modules/agent/agentList";
	}

	@RequiresPermissions("agent:agent:view")
	@RequestMapping(value = "form")
	public String form(Agent agent, Model model) {
		if (agent.getParent()!=null && StringUtils.isNotBlank(agent.getParent().getId())){
			agent.setParent(agentService.get(agent.getParent().getId()));
			// 获取排序号，最末节点排序号+30
			if (StringUtils.isBlank(agent.getId())){
				Agent agentChild = new Agent();
				agentChild.setParent(new Agent(agent.getParent().getId()));
				List<Agent> list = agentService.findList(agent); 
				if (list.size() > 0){
					agent.setSort(list.get(list.size()-1).getSort());
					if (agent.getSort() != null){
						agent.setSort(agent.getSort() + 30);
					}
				}
			}
		}
		if (agent.getSort() == null){
			agent.setSort(30);
		}
		model.addAttribute("agent", agent);
		return "modules/agent/agentForm";
	}

	@RequiresPermissions("agent:agent:edit")
	@RequestMapping(value = "save")
	public String save(Agent agent, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, agent)){
			return form(agent, model);
		}
		agentService.save(agent);
		addMessage(redirectAttributes, "保存代理商成功");
		return "redirect:"+Global.getAdminPath()+"/agent/agent/?repage";
	}
	
	@RequiresPermissions("agent:agent:edit")
	@RequestMapping(value = "delete")
	public String delete(Agent agent, RedirectAttributes redirectAttributes) {
		agentService.delete(agent);
		addMessage(redirectAttributes, "删除代理商成功");
		return "redirect:"+Global.getAdminPath()+"/agent/agent/?repage";
	}

	@RequiresPermissions("user")
	@ResponseBody
	@RequestMapping(value = "treeData")
	public List<Map<String, Object>> treeData(@RequestParam(required=false) String extId, HttpServletResponse response) {
		List<Map<String, Object>> mapList = Lists.newArrayList();
		List<Agent> list = agentService.findList(new Agent());
		for (int i=0; i<list.size(); i++){
			Agent e = list.get(i);
			if (StringUtils.isBlank(extId) || (extId!=null && !extId.equals(e.getId()) && e.getParentIds().indexOf(","+extId+",")==-1)){
				Map<String, Object> map = Maps.newHashMap();
				map.put("id", e.getId());
				map.put("pId", e.getParentId());
				map.put("name", e.getName());
				mapList.add(map);
			}
		}
		return mapList;
	}
	
}